package com.nbot.server.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NbotStringUtil {
    /**
     * 把数字汉字转换成阿拉伯数字
     *
     * @param inStr 输入的字符串
     * @return
     */
    public static String convertHanziToShuzi(String inStr) {
        if (inStr == null) {
            return null;
        }
        inStr = StrUtil.cleanBlank(inStr);
        if ("".equals(inStr)) {
            return "";
        }
        String r = null;
        r = inStr.replaceAll("零", "0")
                .replaceAll("一", "1")
                .replaceAll("二", "2")
                .replaceAll("三", "3")
                .replaceAll("四", "4")
                .replaceAll("五", "5")
                .replaceAll("六", "6")
                .replaceAll("七", "7")
                .replaceAll("八", "8")
                .replaceAll("九", "9")
                .replaceAll("\\u002B", "加")
                .replaceAll("战", "站")
                .replaceAll("桶","筒");//wyzc

        if ((r.charAt(0) == '6' || r.charAt(0) == '流' || r.charAt(0) == '留') &&( r.charAt(1) == '8'||r.charAt(1) == '9') ){
            r = r.replaceFirst("留", "6").replaceFirst("流","6");
        }else if(r.charAt(0)=='6'||r.charAt(0)=='流'||r.charAt(0)=='留'){
            r = r.replaceFirst("留", "柳")
                    .replaceFirst("流","柳")
                    .replaceFirst("6","柳");
        }
        if(r.charAt(0)=='南'&&r.charAt(1)=='十'&&r.charAt(2)=='2'){
            r = r.replaceFirst("2", "二");
        }
        if ((r.charAt(0) == '柳') && r.charAt(1) == '转'){
            r = r.replaceFirst("柳", "6");
        }
        StaticLog.info("查询转换字符结果："+r);
        return r;
    }

    public static String setScale(String result, int newScale) {
        result = result.replace("[", "").replace("]", "");
        if (result == null) {
            return "";
        }
        if ("".equals(StrUtil.cleanBlank(result))) {
            return "";
        }
        try {
            BigDecimal r = new BigDecimal(result).setScale(newScale, BigDecimal.ROUND_HALF_UP);
            result = r.toString();
        } catch (NumberFormatException e) {
            return "";
        }
        return result;
    }

    public static String getAlarmLevelStr(String levelStr) {
        if (levelStr == null) {
            return "";
        }
        if (StrUtil.cleanBlank(levelStr).equals("")) {
            return "";
        }
        if ("1".equals(levelStr)) {
            return "普通";
        } else if ("2".equals(levelStr)) {
            return "重要";
        } else if ("3".equals(levelStr)) {
            return "紧急";
        } else {
            return "";
        }
    }


    public static String getAlarmTypeStr(Integer typeValue) {
        if (typeValue == null) {
            return "";
        }

        if (typeValue == 1) {
            return "低限预警";
        } else if (typeValue == 2) {
            return "低低报警";
        } else if (typeValue == 3) {
            return "高限预警";
        } else if (typeValue == 4) {
            return "高高报警";
        } else {
            return "";
        }
    }
//    (1：低限预警，2：低低报警，3：高限预警，4：高高报警)


    /**
     * 生成今天的日期字符串
     * @return yyyyMMdd格式的字符串
     */
    public static String getTodayStr(){
        Date date = new Date();
        SimpleDateFormat fm = new SimpleDateFormat("yyyyMMdd");
        return fm.format(date);
    }
}
